<template>
	<div>

		<div class="app-container">
			<TopInfo />
			<Search :lookup="data.lookup" @investigate="inves" />

			<div class="w-full relative">
				<el-button type="primary" size="small" @click="add(1)">
					<template #default>
						<span class="text-sm">{{ $t('system.mail.add') }}</span>
					</template>
				</el-button>
			</div>
		</div>
		<div class="app-container" v-loading="data.mtloading">
			<!--发送方配置  -->
			<div>
				<el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }"
					:cell-style="{ 'text-align': 'center' }" class="w-full">
					<el-table-column prop="ec_id" :label="$t('system.mail.configid')" show-overflow-tooltip />
					<el-table-column prop="host" :label="$t('system.mail.smtpSserver')" show-overflow-tooltip />
					<el-table-column prop="username" :label="$t('eventlog.user.account')" show-overflow-tooltip />
					<el-table-column prop="authCode" :label="$t('system.mail.authCode')" show-overflow-tooltip />
					<el-table-column prop="sendEmail" :label="$t('system.mail.emailSende')" show-overflow-tooltip
						width="120" />
					<el-table-column prop="nickname" :label="$t('system.mail.sender')" show-overflow-tooltip
						width="120" />
					<el-table-column prop="replyMail" :label="$t('system.mail.emailReply')" show-overflow-tooltip />
					<el-table-column prop="replyNickname" :label="$t('system.mail.nameReply')" show-overflow-tooltip />
					<el-table-column prop="isHtml" :label="$t('system.mail.HTMLcontent')" show-overflow-tooltip
						width="120">
						<template #default="scope">
							{{ setIsHtml(scope.row || '--') }}
						</template>
					</el-table-column>
					<el-table-column prop="status" :label="$t('public.state')" show-overflow-tooltip width="75">
						<template #default="scope">
							{{ setStatus(scope.row || '--') }}
						</template>
					</el-table-column>
					<el-table-column prop="creator_nickname" :label="$t('source.creater')" show-overflow-tooltip
						width="75" />
					<el-table-column prop="create_time" :label="$t('date.createTime')" width="120">
						<template #default="scope">
							{{ getPlevel.gettime(scope.row.create_time, 3) }}
						</template>
					</el-table-column>
					<el-table-column prop="update_time" :label="$t('date.modifyTime')" width="120">
						<template #default="scope">
							{{ getPlevel.gettime(scope.row.update_time, 3) }}
						</template>
					</el-table-column>
					<el-table-column :label="$t('public.operate')" width="200">
						<template #default="scope">
							<el-button type="primary" size="small" @click="toTemplate(scope.row)">{{
								$t('public.template') }}</el-button>
							<el-button type="primary" size="small" @click="onUpdate(scope.row, 3)">{{
								$t('public.modify') }}</el-button>
							<el-button v-prmis="'email_config_del'" type="info" size="small"
								@click="onDel(scope.row)">{{ $t('public.del') }}</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<PageNum :total="data.total" :page="data.sement" @exposeData="current" @fraction="fraction" />
		</div>
	</div>

</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
const { t: $t } = useI18n()
import TopInfo from '@/components/TopInfo/index.vue';
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import { useRouter } from 'vue-router';
import getPlevel from '@/utils/level';
import arrange from '@/utils/hierarchy';
import { getEmailList, getEmailDel } from '@/api/system-mail'
const router = useRouter();
let data = reactive({
	mtloading: false, // 加载
	lookup: [// 筛选栏
		{ title: $t('system.mail.smtpSserver'), type: 'input', value: '', key: 'host' },
		{ title: $t('eventlog.user.account'), type: 'input', value: '', key: 'username' },
		{ title: $t('system.mail.emailSende'), type: 'input', value: '', key: 'sendEmail' },
	],
	list: [], // 列表数据
	aolist: [],
	sement: { // 筛选数据
		host: '',
		username: '',
		sendEmail: '',
		pageNum: 15,
		page: 1,
		template_type: '',
		subject: '',
		ao_id: ''
	},
	total: 0, // 总条数
})
// 获取接口信息
let geturl = async () => {
	let array = await arrange.arrange(1);
	for (let item of data.lookup2) {
		if (item.key == 'ao_id') {
			item.select = []
			data.aolist = []
			for (let ime of array) {
				item.select.push({
					label: ime.ao_id + ' - ' + ime.organization_name,
					value: ime.ao_id
				})
				data.aolist.push({
					label: ime.organization_name,
					value: ime.ao_id
				})
			}
		}
	}
}
geturl()
let setStatus = (row) => {
	if (row.status == 1) {
		row.statusCn = $t('public.open')
	}
	if (row.status == 2) {
		row.statusCn = $t('public.disable')
	}
	return row.statusCn
}
let setIsHtml = (row) => {
	if (row.isHtml == 1) {
		row.isHtmlCn = $t('public.yes')
	}
	if (row.isHtml == 2) {
		row.isHtmlCn = $t('public.no')
	}
	return row.isHtmlCn
}
// 获取列表信息
let getlist = () => {
	data.mtloading = true
	let pram = {
		host: data.sement.host,
		username: data.sement.username,
		sendEmail: data.sement.sendEmail,
		pageNum: 15,
		page: 1
	}
	getEmailList(pram).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			data.list = res.data.data;
			data.total = res.data.total;
		}
	})
}
let toTemplate = (row: any) => {
	let query = {
		id: row.ec_id
	}
	router.push({ path: '/system/mail/template', query: query });
}
let onUpdate = (row, type) => {
	let query = {
		type: type,
		l: encodeURI(
			JSON.stringify({
				info: row
			})
		)
	}
	router.push({ path: '/system/mail/add', query: query });
}
// 删除
let onDel = (row: any) => {
	ElMessageBox.confirm($t('message.currentData'))
		.then(res => {
			let pram = {
				ec_id: row.ec_id
			}
			getEmailDel(pram).then(res => {
				if (res.state == 200) {
					ElMessage({
						message: $t('message.delSuccess'),
						type: 'success'
					});
					data.dialog = false
					getlist();
					return;
				}
				ElMessage({
					message: $t('message.networkFailed'),
					type: 'error'
				});
			});
		})
		.catch(err => {
			console.log('取消');
		});
};
// 查询回调
let inves = (e) => {
	for (let key in e) {
		data.sement[key] = e[key];
	}
	data.sement.page = 1;
	data.sement.pageNum = 15;
	getlist()
};

// 页数回调
let current = (e) => {
	data.sement.page = e
	getlist()
}

// 条数回调
let fraction = (e) => {
	data.sement.pageNum = e
	getlist()
}

// 广告推送
let add = (type) => {
	let query = {
		type: type
	}
	router.push({ path: '/system/mail/add', query: query });
};
getlist()


</script>
